import { Component, Input, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'date-component',
  templateUrl: 'date-component.html',
  styleUrls: ['date-component.less', 'iconfont.less']
})
export class DateComponent {
  @Input() type: any;
  @Input() dateStart: any;
  @Output() dateStartChange: any = new EventEmitter();
  @Input() dateEnd: any;
  @Output() dateEndChange: any = new EventEmitter();

  constructor() {
  }
  ngOnInit(): void {

    this.showInpyt = 'sectionDate';
    this.actInput = "startDate";
    this.InitializationDate(this.dateStart, 'startDate');
    this.InitializationDate(this.dateEnd, 'endDate');
    this.actDate = this.startDate;
    this.datainit(this.actDate);

  };
  ngOnChanges(changes): void {
  }



  showInpyt = 'sectionDate';
  actInput = "startDate";
  marginLeft = '60px';

  onedata = {
    input: '',
    dateStr: '',
    date: new Date(),
    yeas: new Date().getFullYear(),
    Month: new Date().getMonth(),
    day: new Date().getDate(),
    dayArr: [],
    text: new Date().getDate(),
  };
  startDate = {
    input: "",
    dateStr: '',
    date: new Date(),
    yeas: new Date().getFullYear(),
    Month: new Date().getMonth(),
    day: new Date().getDate(),
    dayArr: [],
    text: new Date().getDate(),
  };
  endDate = {
    input: "",
    dateStr: '',
    date: new Date(),
    yeas: new Date().getFullYear(),
    Month: new Date().getMonth(),
    day: new Date().getDate(),
    dayArr: [],
    text: new Date().getDate(),
  };
  actDate = {
    dateStr: '',
    input: "",
    date: new Date(),
    yeas: new Date().getFullYear(),
    Month: new Date().getMonth(),
    day: new Date().getDate(),
    dayArr: [],
    text: new Date().getDate(),
  };

  InitializationDate(date, type) {
    if ('Invalid Date' == (new Date(date).toString())||date==null||date=="") {
      this[type].input = "";
      this[type].date = new Date();
      return
    }
    this[type].input = (new Date(date).getFullYear()) + "-" + (new Date(date).getMonth() + 1) + "-" + (new Date(date).getDate());
    this[type].date = new Date(date);
    console.log(this[type]);
  }

  datainit(date_opt) {
    date_opt.dateStr = '';
    date_opt.yeas = new Date(date_opt.date).getFullYear();
    date_opt.Month = new Date(date_opt.date).getMonth();
    date_opt.day = new Date(date_opt.date).getDate();
    date_opt.yeas = new Date(date_opt.date).getFullYear();
    date_opt.dayArr = [];
    date_opt.text = new Date(date_opt.date).getDate();

    let MonthStartdate = new Date(date_opt.yeas, date_opt.Month, 0).getDay();
    let MaxDate = new Date(date_opt.yeas, date_opt.Month + 1, -1).getDate() + 1;
    console.log(MonthStartdate);
    let dayArr = [];
    let day = MonthStartdate % 7;
    date_opt.dateStr = date_opt.yeas.toString() + '-' + (date_opt.Month + 1).toString() + '-' + date_opt.text.toString();

    let arr = [];
    let i = 0;

    for (let j = 0; j < 7; j++) {
      let setDate = new Date(date_opt.yeas, date_opt.Month, j - day);
      arr.push({
        dateStr: `${setDate.getFullYear()}-${setDate.getMonth() + 1}-${setDate.getDate()}`,
        yeas: setDate.getFullYear(),
        data: setDate,
        day: setDate.getDay(),
        text: setDate.getDate(),
      });
      i = j - day;
    };
    console.log("周：", day);
    for (let i = 0; i <= Math.ceil((MaxDate - i) / 7); i++) {
      dayArr.push([]);
    }
    dayArr.forEach(item => {
      for (let n = 0; n < 7; n++) {
        i++;
        let setDate = new Date(date_opt.yeas, date_opt.Month, i);
        item.push({
          data: setDate,
          text: setDate.getDate(),
          dateStr: `${setDate.getFullYear()}-${setDate.getMonth() + 1}-${setDate.getDate()}`,
          yeas: setDate.getFullYear(),
          day: setDate.getDay(),
        });
      }
    });

    if (arr.some(res => res.text == '1')) {
      dayArr.unshift(arr);
    }
    if (!dayArr[dayArr.length - 1].some(res => res.text == '1')) {
      dayArr.pop();
    };
    dayArr[0].forEach(element => {
      element['noSameMonth'] = element.text < 10 ? false : true;
    });
    dayArr[dayArr.length - 1].forEach(element => {
      element['noSameMonth'] = element.text < 10 ? true : false;
    });
    console.log(dayArr);
    date_opt.dayArr = dayArr;
    this.dayArr = dayArr;
    date_opt.dateStr = date_opt.input;
  }
  dayArr = [];
  /**
   * @param e    inpu对象
   * @param type date 类型
   */
  inputfocus(e, type) {
    this.actDate = this[type];
    this.actInput = type;
    this.dayArr = this[type].dayArr;
    if ('Invalid Date' == (new Date(this[type].input).toString())) {
      this[type].date = new Date();
      this.datainit(this[type]);
    } else {
      this[type].date = new Date(this[type].input);
      this.datainit(this[type]);
    }
    if (type == 'startDate') {
      this.marginLeft = '60px';
    }
    if (type == 'endDate') {
      this.marginLeft = '160px';
    }


    console.log(e);
  };
  inputblur(e, type) {
    // this.actInput = type;

    let changedate = (new Date(this[type].input).toString()) == 'Invalid Date' ? null : (new Date(this[type].input));

    if (type == 'startDate') {
      this.dateStartChange.emit(changedate);
    }
    if (type == 'endDate') {
      this.dateEndChange.emit(changedate);
    }
    if ('Invalid Date' == (new Date(this[type].input).toString())) {
      console.log('请输入合法日期');
      return
    } else {
      this[type].date = new Date(this[type].input);
      this.datainit(this[type]);
    }
  };
  act_dataSpan(date, type) {
    this[type].date = date.data;
    this[type].input = date.dateStr;
    this.datainit(this[type]);

    let changedate = (new Date(this[type].input).toString()) == 'Invalid Date' ? null : (new Date(this[type].input));
    
    if (type == 'startDate') {
      this.dateStartChange.emit(changedate);
    }
    if (type == 'endDate') {
      this.dateEndChange.emit(changedate);
    }

  }

  monthadd(num) {
    let Month = this.actDate.date.getMonth() + num;
    this.actDate.date = new Date(this.actDate.date.setMonth(Month));
    this.datainit(this.actDate);
    if ('Invalid Date' == (new Date(this.actDate.input).toString())) {
      this.actDate.dateStr = "";
      console.log('请输入合法日期');
    } else {
      this.actDate.dateStr = this.actDate.input;
    }
  }

  yeashadd(num) {
    let yeas = this.actDate.date.getFullYear() + num;
    this.actDate.date = new Date(this.actDate.date.setFullYear(yeas));
    this.datainit(this.actDate);
    if ('Invalid Date' == (new Date(this.actDate.input).toString())) {
      this.actDate.dateStr = "";
      console.log('请输入合法日期');
    } else {
      this.actDate.dateStr = this.actDate.input;
    }
  }
}
